// pages/orders/orders.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderArr:[],
    custom:'',
    shareid:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // options.custom='小毕车队'
//     this.loadshareorder('861,862')
// return false
    this.setData({
      custom:options.custom
    })
    if (options.custom){
      this.loadorder(options.custom)
    }else{
      this.loadshareorder(options.orderid)
    }
  },
  handleCk(e) {
    let id = e.currentTarget.dataset['id'];
    let tempArray = this.data.orderArr.map(o => {
      if(o.id == id) {
        if(o.checked == undefined) {
          o.checked = true
        } else {
          o.checked = !o.checked
        }
      }
      return o
    })
    
    this.setData({
      orderArr: tempArray
    })

  },
  //查看订单详情
  turnDetail: function (e) {
    wx.navigateTo({
      url: '../index/index?orderid=' + e.currentTarget.dataset.uuid,
    })
  },
  loadorder: function (custom) {
    var that = this;
    wx.request({
      url: app.globalData.evaluateurl + 'apint/bill/order/selectOrderCus',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'post',
      data: {
        customer: custom
      },
      success: function (res) {
        if (res.statusCode == 200){
          var data = res.data;
          for(var i=0;i<data.length;i++){
            var factoryAddress = data[i].factoryAddress
            data[i].factoryAddress = factoryAddress.substr(0, 6) + '******'
          }
          that.setData({
            orderArr: res.data
          }) 
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) {
        
      }
    })
  },
  loadshareorder: function (orderid) {
    var that = this;
    wx.request({
      url: app.globalData.evaluateurl + 'apint/bill/order/selectShareOrders',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'post',
      data: {
        ids: orderid
      },
      success: function (res) {
        if (res.statusCode == 200) { 
          var data = res.data;
          console.log(res);
          
          for (var i = 0; i < data.length; i++) {
            var factoryAddress = data[i].factoryAddress
            data[i].factoryAddress = factoryAddress.substr(0, 6) + '******'
          }
          that.setData({
            orderArr: res.data
          })
          console.log(that.data.orderArr);
          
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) {

      }
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.loadorder(this.data.custom)
    wx.stopPullDownRefresh()
  },
  bindCheckbox: function (e) {//勾
    console.log(e)
    /*绑定点击事件，将checkbox样式改变为选中与非选中*/
    //拿到下标值，以在carts作遍历指示用
    var index = parseInt(e.currentTarget.dataset.index);
    //原始的icon状态
    var selected = this.data.orderArr[index].selected;
  },
  shareFunc() {
    wx.updateShareMenu({
      withShareTicket: true,
      success() { }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    let tempArray = []
    that.data.orderArr.forEach(item => {
      if(item.checked) {
        tempArray.push(item.id)
      }
    });

    // let orders = that.data.shareid; 
    let orders = tempArray.join(',')
    // let orders2 =orders.toString
    // console.log(orders2,orders);
    
    if (res.from === 'button') {
      return {
        path: 'pages/orders/orders?orderid=' + orders
      }
    }
    
  },
})